//
//  MainTabBC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/25.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MainTabBC.h"
#import "MainNavC.h"
#import "HomeTabVC.h"
#import "DemandTheHallViewController.h"
#import "ServiceTheHallViewControl.h"
#import "ReleaseShowView.h"
#import "ServiceTheHallVC.h"
#import "MyCollectionVC.h"
#import "CustomeTabBar.h"
#import "UIView+TYAlertView.h"
#import "WriteRequirementTabVC.h"
#import "ReleaseServiceTabVC.h"
#import "RealAuthenticationVC.h"
@interface MainTabBC ()<CustomeTabBarDelegate,UITabBarControllerDelegate>
@property(nonatomic,strong) HomeTabVC *homeTabVC;
@property(nonatomic,strong) DemandTheHallViewController *demandVC;
@property(nonatomic,strong) ServiceTheHallViewControl *serviceVC;
@property(nonatomic,strong) MyCollectionVC *myCollectionVC;
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,strong) ReleaseShowView *showView;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) WriteRequirementTabVC *writeVC;
@property(nonatomic,strong) ReleaseServiceTabVC *releaseVC;
@property(nonatomic,assign) NSInteger selected;
@property(nonatomic,assign) NSInteger tag;
@property(nonatomic,strong) CustomeTabBar *customeTabB;
@end

@implementation MainTabBC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.homeTabVC = [[HomeTabVC alloc]init];
    [self addChildViewControllers:self.homeTabVC withNormalImage:@"replace_icon_71" withHighLightImage:@"replace_icon_62" withTitle:@""];
    self.demandVC = [[DemandTheHallViewController alloc]init];
    [self addChildViewControllers:self.demandVC withNormalImage:@"replace_icon_65" withHighLightImage:@"replace_icon_73" withTitle:@""];
    self.serviceVC = [[ServiceTheHallViewControl alloc]init];
    [self addChildViewControllers:self.serviceVC withNormalImage:@"replace_icon_41" withHighLightImage:@"replace_icon_75" withTitle:@""];
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.myCollectionVC = [[MyCollectionVC alloc]initWithCollectionViewLayout:flow];
    [self addChildViewControllers:self.myCollectionVC withNormalImage:@"replace_icon_77" withHighLightImage:@"replace_icon_69" withTitle:@"我的"];
    self.customeTabB = [[CustomeTabBar alloc]initWithFrame:self.tabBar.bounds];
    self.customeTabB.delegate = self;
    XLog(@"self.tabBar.bounds:%@",NSStringFromCGRect(self.tabBar.bounds));
    XLog(@"self.tabBar.bounds.size.height:%f",self.tabBar.bounds.size.height);
    XLog(@"self.frma:%f",self.view.frame.size.height);
    //[self setValue:tabBar forKey:@"tabBar"];
    [self setValue:self.customeTabB forKey:@"tabBar"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTokenMainTabBar:) name:@"checkTokenMainTabBar" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTokenError:) name:@"checkTokenError" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedIndex:) name:@"selectedIndex" object:nil];
}

-(void)addChildViewControllers:(UIViewController *)childController withNormalImage:(NSString *)normalImage withHighLightImage:(NSString *)highLightImage withTitle:(NSString *)title
{
    MainNavC *mainNavC = [[MainNavC alloc]initWithRootViewController:childController];
    childController.title = title;
    childController.tabBarItem.image = [UIImage imageNamed:normalImage];
    XLog(@"childController.tabBarItem.image:%@",childController.tabBarItem.image);
    childController.tabBarItem.selectedImage = [UIImage imageNamed:highLightImage];
    [self addChildViewController:mainNavC];
    
}
#pragma mark-代理:寻找需求,服务
-(void)selectedIndex:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"selectedIndex:%@",dict);
    NSInteger index = [[info.userInfo objectForKey:@"selectedIndex"] integerValue];
    MCButton *btn = (MCButton *)[self.customeTabB viewWithTag:index];
    [self.customeTabB clickBtn:btn];
}

-(void)customeTabBar:(CustomeTabBar *)customeTabBar withInterget:(NSInteger)interger
{
    XLog(@"interger:%ld",(long)interger);
    if (interger< 202) {
        self.selectedIndex = interger - 200;
    }else if (interger == 202) {
//        self.selectedIndex = interger - 200;
        self.showView = [[ReleaseShowView alloc]initWithFrame:kUIScreen];
        self.showView.frame = CGRectMake(0, kUIScreen.size.height, kUIScreen.size.width, kUIScreen.size.height);
        UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        [viewController.view addSubview:self.showView];
        [UIView animateWithDuration:0.3 animations:^{
            self.showView.transform = CGAffineTransformMakeTranslation(0, -kUIScreen.size.height);
        }];
        __weak typeof (self)weakSelf = self;
        self.writeVC = [[WriteRequirementTabVC alloc]init];
        self.writeVC.returnValueBlock = ^(NSInteger status) {
             weakSelf.showView.transform = CGAffineTransformIdentity;
        };
        self.releaseVC = [[ReleaseServiceTabVC alloc]init];
        self.releaseVC.returnValueBlock = ^(NSInteger selected) {
            weakSelf.showView.transform = CGAffineTransformIdentity;
        };
        self.showView.returnValueBlock = ^(NSInteger selected) {
            weakSelf.selected = selected;
            weakSelf.tag = selected;
            if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]) {
                [UIAlertViewWarningAccount showWarningAccount];
                LogInOrOutVC *logVC = [[LogInOrOutVC alloc]init];
                MainNavC *mianNav = [[MainNavC alloc]initWithRootViewController:logVC];
                [weakSelf presentViewController:mianNav animated:YES completion:^{
                    weakSelf.showView.transform = CGAffineTransformIdentity;
                }];
            }else if ([UserDefaultsHelper readAutoLogIn].length == 0) {
                [UIAlertViewWarningAccount showWarningAccount];
                LogInOrOutVC *logVC = [[LogInOrOutVC alloc]init];
                MainNavC *mianNav = [[MainNavC alloc]initWithRootViewController:logVC];
                [weakSelf presentViewController:mianNav animated:YES completion:^{
                    weakSelf.showView.transform = CGAffineTransformIdentity;
                }];
            }else {
                if (selected == 1) {
                    //弹出发布需求,隐藏showView
                    MainNavC *mainNav = [[MainNavC alloc]initWithRootViewController:weakSelf.writeVC];
                    [weakSelf  presentViewController:mainNav animated:YES completion:^{
                        weakSelf.showView.transform = CGAffineTransformIdentity;
                    }];
                }else {
                    weakSelf.dict = @{
                                      @"userId":[UserDefaultsHelper readUid],
                                      @"token":[UserDefaultsHelper readToken],
                                      };
                    [CustomeManager POSTCheckTokenWithURLIndex:0 parameters:weakSelf.dict success:^(NSDictionary *dict) {
                        
                    } failure:^(NSError *error) {
                        
                    }];
                }
            }
        };
    }else {
        self.selectedIndex = interger-1 - 200;
    }
    if (self.index == interger) {
        //    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
        //    [WSProgressHUD show];
        if (interger == 200) {
            [self.homeTabVC reloadInternet];
        }else if(interger == 201){
        }else if (interger == 202){
            
        }else if (interger == 203){
            [self.serviceVC reloadInternet];
        }else {
            [self.myCollectionVC reloadInternet];
        }
    }
    self.index = interger;
}
-(void)checkTokenMainTabBar:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    NSInteger status = [[info.userInfo objectForKey:@"status"] integerValue];
    __weak typeof (self)weakSelf = self;
    if (status == 200) {
        XLog(@"[UserDefaultsHelper readType]:%@",[UserDefaultsHelper readType]);
        if ([[UserDefaultsHelper readType] integerValue] <= 0) {
            if ([[UserDefaultsHelper readType] integerValue] < 0) {
                [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"实名认证在审核中,请耐心等待"];
                self.showView.transform = CGAffineTransformIdentity;
            }else {
                TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"请进行实名认证" message:@""];
                [alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancel handler:^(TYAlertAction *action) {
                    weakSelf.showView.transform = CGAffineTransformIdentity;
                }]];
                
                [alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
                    RealAuthenticationVC *vc = [[RealAuthenticationVC alloc]init];
                    MainNavC *mainVC = [[MainNavC alloc]initWithRootViewController:vc];
                    //                [mainVC addChildViewController:vc];
                    [weakSelf presentViewController:mainVC animated:YES completion:^{
                        self.showView.transform = CGAffineTransformIdentity;
                    }];
                    
                }]];
                [alertView showInWindowWithOriginY:200 backgoundTapDismissEnable:YES];
                
            }
        }else {
            if (self.selected == 1) {
            }else {
                MainNavC *mainNav = [[MainNavC alloc]initWithRootViewController:self.releaseVC];
                [self  presentViewController:mainNav animated:YES completion:^{
                    self.showView.transform = CGAffineTransformIdentity;
                }];
            }
        }
    }
    
}
-(void)checkTokenError:(NSNotification *)info
{
    XLog(@"tabbar");
    [LogOutAccount presentOutAccount];
    [UIAlertViewWarningAccount showWarningAccount];
}
-(void)customeTabBar:(CustomeTabBar *)customeTabBar withCustomeBtn:(MCButton *)customeBtn
{
//    self.releaseVC = [[ReleaseVC alloc]init];
//    [self presentViewController:self.releaseVC animated:YES completion:nil];
    WriteRequirementTabVC *writeVC = [[WriteRequirementTabVC alloc]init];
    ReleaseServiceTabVC *releaseVC = [[ReleaseServiceTabVC alloc]init];
    
    self.showView = [[ReleaseShowView alloc]initWithFrame:kUIScreen];
    self.showView.frame = CGRectMake(0, kUIScreen.size.height, kUIScreen.size.width, kUIScreen.size.height);
    UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [viewController.view addSubview:self.showView];
    [UIView animateWithDuration:0.3 animations:^{
        self.showView.transform = CGAffineTransformMakeTranslation(0, -kUIScreen.size.height);
    }];
    __weak typeof (self)weakSelf = self;
    self.showView.returnValueBlock = ^(NSInteger selected) {
        if (selected == 1) {
            MainNavC *mainNav = [[MainNavC alloc]initWithRootViewController:writeVC];
            [weakSelf  presentViewController:mainNav animated:YES completion:^{
                weakSelf.showView.transform = CGAffineTransformIdentity;
            }];
            
        }else {
            MainNavC *mainNav = [[MainNavC alloc]initWithRootViewController:releaseVC];
            [weakSelf  presentViewController:mainNav animated:YES completion:^{
                weakSelf.showView.transform = CGAffineTransformIdentity;
            }];
        }
    };
}
- (void)viewDidLayoutSubviews
{
    [self removeSystemBarbtn];

}
//移除系统tabbar上面的按钮(UITabBarButton类型)
- (void)removeSystemBarbtn{

    Class class =  NSClassFromString(@"UITabBarButton");
    for (UIView *view in self.tabBar.subviews) {
        //如果view是由UITabBarButton类创建的对象,则将其移除
        if ([view isKindOfClass:class]) {
            [view removeFromSuperview];
        }

    }

}
@end
